<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { BaseFormConfig } from '../../config'
import { getAreaObj } from '@/utils/util'
import { cloneDeep } from 'lodash-es'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { getValue } = useOperate(1)
const { setTempParams } = useCommonStore()

const rowData: any = inject('nowRowData')
let dpServiceObjects = rowData.value?.data?.dpServiceObjects

const emit = defineEmits(['submit'])

let baseData = rowData.value?.baseData?.dpServiceObjects
const formModel = ref({
	...cloneDeep(dpServiceObjects),
})

const formRef = ref()
const onSubmit = (form, done) => {
	const areaObj: any = getAreaObj(form.areas)
	const areaNameObj: any = getAreaObj(form.areas_name)
	const crAreasObj: any = getAreaObj(form.crAreas, 'cr')
	const crAreasNameObj: any = getAreaObj(form.cr_areas, 'cr')
	const params = {
		...form,
		...areaObj,
		...crAreasObj,
		...areaNameObj,
		...crAreasNameObj,
    buildInfo:undefined,
		code: 1001,
	}
  if(form.buildInfo) {
    params.moveInBuilding = form.buildInfo.join(',')
  }

	// 变更字段
	let changeColumnsBase = BaseFormConfig.column.filter((item) => item.changeField)
	// 处理变更数据
	let changeData = []
	changeColumnsBase.map((item) => {
		const tempBefore = baseData[item.propName || item.prop]
		const tempAfter = params[item.propName || item.prop]
		if (tempBefore != tempAfter) {
			changeData.push({
				label: item.label,
				before: getValue(tempBefore, item),
				after: getValue(tempAfter, item),
			})
		}
	})
	const emptyObj = {
		areas_name: undefined,
		cr_areas: undefined,
		areas: undefined,
		crAreas: undefined,
		code: undefined,
	}
	const changeObj = {
		data: changeData,
		dataChangeContentList: [
			{
				changeBefore: {
					...baseData,
					...emptyObj,
				},
				changeAfter: {
					...params,
					...emptyObj,
				},
				changeData: {
					...params,
					...emptyObj,
				},
				changeType: '服务对象',
				code: 1001,
			},
		],
	}
	setTempParams({
		name: params.name,
		idCard: params.idCard,
		nowChange: changeObj,
	})
	emit('submit', changeObj)
	done()
}

const onSave = () => {
	formRef.value!.onSubmit()
}

defineExpose({
	onSave,
})
</script>

<style scoped></style>
